﻿using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    public partial class Frm_AddUser : Form
    {
        #region 属性
        /// <summary>
        /// 按钮状态枚举
        /// </summary>
        public enum EStatus
        {
            Add = 0,
            Modify = 1,
        }

        /// <summary>
        /// 点击按钮状态
        /// </summary>
        public EStatus FStatus { get; set; }

        /// <summary>
        /// 保存执行事件
        /// </summary>
        public event EventHandler SaveEvent;

        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserID { get; set; }

        /// <summary>
        /// 部门ID
        /// </summary>
        public Guid DeptID { get; set; }
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_AddUser(Guid departmentID,EStatus status)
        {
            InitializeComponent();
            this.FStatus = status;
            this.DeptID = departmentID;
        }
        public Frm_AddUser(Guid departmentID, Guid userID, EStatus status)
        {
            InitializeComponent();
            this.FStatus = status;
            this.DeptID = departmentID;
            this.UserID = userID;
        }
        #endregion

        #region 窗体加载完成
        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddUser_Load(object sender, EventArgs e)
        {
            if (FStatus == EStatus.Modify)
            {
                AppService aservice = new AppService();
                Users user = aservice.GetUserByID(UserID);
                UserNameEdit.Text = user.UName;
                TrueNameEdit.Text = user.TrueName;
                IsEnabled.Checked = user.IsEnabled;
                this.Text = "用户 - 编辑";
            }
        }
        #endregion

        #region 按钮事件
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            Users user = null;

            if (FStatus == EStatus.Add)
            {
                user = aservice.AddUser(this.UserNameEdit.Text,this.DeptID,this.TrueNameEdit.Text,this.IsEnabled.Checked);
                MessageBox.Show("添加成功!");
            }
            if (FStatus == EStatus.Modify)
            {
                user = aservice.UpdateUsers(this.UserID, this.UserNameEdit.Text, this.TrueNameEdit.Text, this.IsEnabled.Checked);
                MessageBox.Show("修改成功!");
            }
            if (SaveEvent != null)
            {
                SaveEvent(user,new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

    }
}
